<template>
  <div class='search'>
    <a-descriptions bordered size='small' :column='3'>
      <a-descriptions-item label='单号'>
        {{ formItem.orderNo }}
      </a-descriptions-item>
      <a-descriptions-item label='制单人'>
        {{ formItem.nickName }}
      </a-descriptions-item>
      <a-descriptions-item label='发货日期'>
        {{ formItem.sendDate }}
      </a-descriptions-item>
      <a-descriptions-item label='订单日期'>
        {{ formItem.createTime }}
      </a-descriptions-item>
      <a-descriptions-item label='部门'>
        {{ formItem.deptName }}
      </a-descriptions-item>
      <a-descriptions-item label='业务员'>
        {{ formItem.saleManName }}-{{ formItem.linkTel }}
      </a-descriptions-item>
      <a-descriptions-item label='供货方'>
        {{ formItem.supplierName }}
      </a-descriptions-item>
      <a-descriptions-item label='购货方'>
        {{ formItem.customName }}
      </a-descriptions-item>

      <a-descriptions-item label='供方资信'>
        <a-tag color='#2db7f5' v-if='formItem.supplierBondType==0'>不垫付</a-tag>
        <a-tag color='purple' v-if='formItem.supplierBondType==1'>底价</a-tag>
        <a-tag color='purple' v-if='formItem.supplierBondType==2'>底价+税金</a-tag>
        <a-tag color='purple' v-if='formItem.supplierBondType==3'>税金</a-tag>
        <a-tag color='purple' v-if='formItem.supplierBondType==6'>底价/2+税金</a-tag>
        <a-tag color='purple' v-if='formItem.supplierBondType==5'>全额垫付</a-tag>
        <a-tag color='purple' v-if='formItem.supplierBondType==8'>销售金额*费率</a-tag>

      </a-descriptions-item>
      <a-descriptions-item label='购方资信'>
        <a-tag color='#2db7f5' v-if='formItem.customBondType==0'>全款</a-tag>
        <a-tag color='purple' v-if='formItem.customBondType==1'>底价</a-tag>
        <a-tag color='purple' v-if='formItem.customBondType==2'>底价+税金</a-tag>
        <a-tag color='purple' v-if='formItem.customBondType==6'>底价/2+税金</a-tag>
        <a-tag color='purple' v-if='formItem.customBondType==3'>税金</a-tag>
        <a-tag color='purple' v-if='formItem.customBondType==5'>全额资信</a-tag>
        <a-tag color='purple' v-if='formItem.customBondType==7'>预付款</a-tag>
      </a-descriptions-item>

      <a-descriptions-item label='订单状态'>
        <a-tag color='#87d068' v-if='formItem.orderState==0'>未提交</a-tag>
        <a-tag color='#87d068' v-if='formItem.orderState==1'>{{ formItem.taskName }}</a-tag>
        <a-tag color='#f50' v-if='formItem.orderState==2'>已审批</a-tag>
      </a-descriptions-item>

      <a-descriptions-item label='收款金额'>
        {{ formItem.takeAmount }} <span v-if='formItem.customOverBond>0'>(多收金额:{{formItem.customOverBond}})</span>
      </a-descriptions-item>
      <a-descriptions-item label='收款账户'>
        {{ formItem.bankName }}
      </a-descriptions-item>
      <a-descriptions-item label='收款日期' :span="2">
        {{ formItem.takeDate }}
      </a-descriptions-item>
      <a-descriptions-item label='备注' :span="3">
        {{ formItem.remark }}
      </a-descriptions-item>
      <a-descriptions-item label='附件' :span="3">
        <f-images :value="formItem.orderImage"></f-images>
      </a-descriptions-item>
    </a-descriptions>
    <a-divider orientation='left'>订单商品</a-divider>
    <f-table ref='orderDetails' rowKey='orderDetailsId' :columns='orderDetailsColumns' :scroll='{x:true}'  size='small' :params='queryParam' :api='api.bod.selectOrderInfoDetails' :autoLoad='false' :showPagination='false' :multiple='false'>
      <span slot='saleType' slot-scope='text, record'>
        <template>
        <span>
          <a-tag v-if='text==1' color='blue'>销售</a-tag>
          <a-tag v-if='text==2' color='blue'>退货</a-tag>
          <a-tag v-if='text==4' color='blue'>票折</a-tag>
          <a-tag v-if='text==3' color='blue'>补货</a-tag>
        </span>
        </template>
      </span>
      <span slot='drugsLotNo' slot-scope='text, record'>
        <template>
        <span>
          <a-tag v-if="text==''" color='blue' @click='updateLotNo(record)'>暂无批号</a-tag>
          <a-tag v-if="text!=''" color='blue' @click='updateLotNo(record)'>{{ record.drugsLotNo }}</a-tag>
        </span>
        </template>
      </span>
      <span slot='action' slot-scope='text, record'>
          <template>
            <a-button size='small'  @click='$refs.orderCalcuateDrawer.showDrawer(record,false)'>发票信息</a-button>
            <a-button size='small' type='danger' @click='$refs.orderCalcuateDrawer.showDrawer(record,true)' style='margin-left: 3px' v-if='formItem.calculate'>核算</a-button>
          </template>
        </span>
    </f-table>
    <a-divider orientation='left' v-if='formItem.delivery==2'>回款记录</a-divider>
    <f-table ref='returnDetails' :columns='returnColumns' rowKey='orderDetailsId' :scroll='{x:true}' :params='queryParam' :api='api.bod.returnList' :autoLoad='false' :showPagination='false' :multiple='false' v-if='formItem.delivery==2'>
      <span slot='hkImage' slot-scope='text, record, index'>
          <f-images :value='record.hkImage'></f-images>
        </span>
      <span slot='returnState' slot-scope='text, record'>
        <template>
        <span>
          <a-tag v-if='text==1' color='blue'>已认款</a-tag>
          <a-tag v-if='text==0' color='blue'>未认款</a-tag>
          <a-tag v-if='text==2' color='blue'>已回款</a-tag>
        </span>
        </template>
      </span>
      <span slot='action' slot-scope='text, record'>
          <template>
<!--            <a-tag size='small' v-if='record.customReturnState==1' type='danger' @click='updateSettle(record)'>更新结算</a-tag>-->
          </template>
        </span>
    </f-table>
    <a-divider orientation='left' v-if='formItem.supplierBondType>0&&supplierBond.paymentNo'>垫付单</a-divider>
    <a-descriptions bordered size='small' layout='vertical' :column='3' v-if='formItem.supplierBondType>0&&supplierBond.paymentNo'>
      <a-descriptions-item label='单号'>
        {{ supplierBond.paymentNo }}
      </a-descriptions-item>
      <a-descriptions-item label='收款单位'>
        {{ supplierBond.supplierName }}
      </a-descriptions-item>
      <a-descriptions-item label='收款人'>
        {{ supplierBond.linkMan }}
      </a-descriptions-item>
      <a-descriptions-item label='收款银行'>
        {{ supplierBond.bankName }}
      </a-descriptions-item>
      <a-descriptions-item label='收款账户'>
        {{ supplierBond.bankNo }}
      </a-descriptions-item>
      <a-descriptions-item label='垫付金额'>
        {{ supplierBond.amount }}
      </a-descriptions-item>
      <a-descriptions-item label='支付人'>
        {{ supplierBond.payUserName }}
      </a-descriptions-item>
      <a-descriptions-item label='支付账户'>
        {{ supplierBond.payBankName }}
      </a-descriptions-item>
      <a-descriptions-item label='支付日期'>
        {{ supplierBond.payDate }}
      </a-descriptions-item>
      <a-descriptions-item label='附件' >
        <f-images :value='supplierBond.paymentImage'></f-images>
      </a-descriptions-item>
      <a-descriptions-item label='备注' >
        {{ supplierBond.remark }}
      </a-descriptions-item>
    </a-descriptions>

    <a-divider orientation='left' v-if='formItem.paymentList&&formItem.paymentList.length>0'>已结算&退保证金记录</a-divider>
    <f-table ref='settleDetails' v-if='formItem.paymentList&&formItem.paymentList.length>0' rowKey='paymentId' :columns='settleColumns' :scroll='{x:true}' size='small' :dataSource='formItem.paymentList' :showPagination='false' :multiple='false'>
         <span slot="amountType" slot-scope="text, record, index">
            <a-tag color="#f50" v-if="text=='CbReturn'">退市场保证金</a-tag>
            <a-tag color="#87d068" v-if="text=='Settle'">市场结算</a-tag>
        </span>
        <span slot='paymentState' slot-scope='text, record, index'>
            <a-tag color='#87d068' v-if='text==0'>未提交</a-tag>
            <a-tag color='#87d068' v-if='text==1' @click='processImg(record)'>审批中</a-tag>
            <a-tag color='#f50' v-if="text==2&&record.amountType=='Settle'" @click='processImg(record)'>已结算</a-tag>
            <a-tag color='#f50' v-if="text==2&&record.amountType=='CbReturn'" @click='processImg(record)'>已退款</a-tag>
        </span>
    </f-table>
    <order-calcuate-drawer ref='orderCalcuateDrawer'></order-calcuate-drawer>
    <process-image ref='processImg' v-if='processImageVisible' :visible.sync='processImageVisible' :processInstanceId='processInstanceId'></process-image>
    <select-drugs-lot-modal ref='drugsLotModal' @drugsSelectChange='drugsLotSelectChange'></select-drugs-lot-modal>
  </div>
</template>
<script>
import api from "./js/api"
import OrderCalcuateDrawer from "./calculate/order_calculate_drawer"
import ProcessImage from "@/views/module/flowable/process/process_image"
import SelectDrugsLotModal from "../drugs/select_drugslot_modal"

export default {
  name: "orderInfo",
  props: {},
  data() {
    return {
      refs: this.$refs,
      loadDept: false,
      queryParam: { orderId: "" },
      api: Object.assign(api, this.api),
      isAdd: true,//是否是添加模式
      loaded: false,//是否加载完成
      orderDetailsId: null,
      processImageVisible: false,
      returnColumns: [],
      settleColumns: [
        {
          title: "#",
          width: "60px",
          align: "center",
          customRender: (text, record, index) => index + 1
        },
        {
          title: "单号",
          align: "center",
          width: "140px",
          dataIndex: "paymentNo"
        },
        {
          title: '单据类型',
          align: 'center',
          dataIndex: 'amountType',
          scopedSlots: { customRender: 'amountType' }
        },
        {
          title: "收款银行",
          align: "center",
          ellipsis: true,
          width: "150px",
          dataIndex: "bankName"
        },
        {
          title: "收款人",
          align: "center",
          width: "120px",
          dataIndex: "saleManName"
        },
        {
          title: "收款账户",
          align: "center",
          width: "150px",
          ellipsis: true,
          dataIndex: "bankNo"
        },
        {
          title: "结算金额",
          align: "center",
          width: "120px",
          dataIndex: "amount"
        },
        {
          title: "申请日期",
          align: "center",
          width: "120px",
          dataIndex: "amountDate"
        },
        {
          title: "支付日期",
          align: "center",
          width: "120px",
          dataIndex: "payDate"
        },
        {
          title: "状态",
          align: "center",
          width: "120px",
          dataIndex: "paymentState",
          scopedSlots: { customRender: "paymentState" }
        }
      ],
      orderDetailsColumns: [],
      formItem: this.getFormItem(),
      supplierBond: {}
    }
  },
  methods: {
    getFormItem() {
      return {
        orderId: "",
        orderNo: "",
        delivery: "",
        saleManName: "",
        customName: "",
        deptName: "",
        saleAmount: "",
        takeAmount: "",
        sendDate: "",
        invoiceSendType: 0,
        reputation: "",
        totalReturnNum: 0,
        paymentList: null,
        reputationAmount: "",
        takeDate: "",
        bankId: "",
        customBondType: 0,
        supplierBondType: 0,
        customId: "",
        billsType: "",
        calculate: false,
        remark: ""
      }
    },
    getReturnColumns() {
      let columns = []
      let column1 = [{
        title: "药品名称",
        align: "center",
        dataIndex: "drugsName"
      }, {
        title: "规格",
        align: "center",
        dataIndex: "drugsSpec"
      }, {
        title: "回款数量",
        align: "center",
        dataIndex: "saleNum"
      },  {
        title: "回款金额",
        align: "center",
        dataIndex: "saleAmount"
      }, {
        title: "预付款金额",
        align: "center",
        dataIndex: "customYsk"
      }, {
        title: "回款账户",
        align: "center",
        dataIndex: "returnBank"
      }, {
        title: "回款日期",
        align: "center",
        dataIndex: "returnDate"
      }, {
        title: "回款状态",
        align: "center",
        dataIndex: "customReturnState",
        scopedSlots: { customRender: "returnState" }
      }, {
        title: "附件",
        align: "center",
        dataIndex: "hkImage",
        scopedSlots: { customRender: "hkImage" }
      },{
        title: "备注",
        align: "center",
        ellipsis: true,
        dataIndex: "remark"
      }
      // , {
      //   title: "操作",
      //   align: "center",
      //   width: "80px",
      //   fixed: "right",
      //   scopedSlots: { customRender: "action" }
      // }
      ]

      if (this.formItem.customBondType != 7) {
        column1.splice(5, 1)
      }
      return Object.assign(columns, column1)
    },
    getOrderDetailsColumns() {
      let column = [{
        title: "操作",
        align: "center",
        width: "90px",
        fixed: "left",
        scopedSlots: { customRender: "action" }
      },{
        title: "商品名称",
        align: "center",
        dataIndex: "drugsName"
      }, {
        title: "规格",
        align: "center",
        dataIndex: "drugsSpec"
      }, {
        title: "批号",
        align: "center",
        dataIndex: "drugsLotNo",
        scopedSlots: { customRender: "drugsLotNo" }
      }, {
        title: "类型",
        align: "center",
        dataIndex: "saleType",
        scopedSlots: { customRender: "saleType" }
      }, {
        title: "日期",
        align: "center",
        dataIndex: "saleDate"
      }, {
        title: "数量",
        align: "center",
        dataIndex: "saleNum"
      }, {
        title: "单价",
        align: "center",
        dataIndex: "salePrice"
      }, {
        title: "销售金额",
        align: "center",
        dataIndex: "saleAmount"
      }]
      if (this.formItem.supplierBondType > 0) {
        column.push({
          title: "垫付款金额",
          align: "center",
          dataIndex: "supplierBond"
        })
      }
      if (this.formItem.customBondType != 0 && this.formItem.customBondType != 7 && this.formItem.customBondType != 5) {
        column.push({
          title: "资保金应收",
          align: "center",
          dataIndex: "customBond"
        })
      }

        column.push({
          title: "预付款金额",
          align: "center",
          dataIndex: "customYsk"
        })


        column.push({
          title: "回款金额",
          align: "center",
          dataIndex: "returnAmount"
        })

      column.push({
        title: "回款日期",
        align: "center",
        dataIndex: "returnDate"
      })
      column.push({
        title: "应返金额",
        align: "center",
        dataIndex: "supplierRetable"
      })
      column.push({
        title: "应返金额2",
        align: "center",
        dataIndex: "supplierRetable2"
      })
      column.push({
        title: "结算金额",
        align: "center",
        dataIndex: "settleAmount"
      })
      if(this.formItem.settleType)
      {
        column.push({
          title: "结算金额2",
          align: "center",
          dataIndex: "settleAmount2"
        })
        column.push({
          title: "结算金额3",
          align: "center",
          dataIndex: "settleAmount3"
        })
      }

      column.push({
        title: "结算高开税金",
        align: "center",
        dataIndex: "customSettleGapUpAmount"
      })
      return column
    },
    onSuccess(data) {
      this.customRefundData = data
    },
    updateLotNo(record) {
      this.orderDetailsId = record.orderDetailsId
      this.$refs["drugsLotModal"].open(record.drugsId)
    },
    //批号选择回调
    drugsLotSelectChange(selectedRowKeys, selectedRows) {
      let drugsLotId = selectedRowKeys[0]
      this.http.post(this, {
        url: this.api.bod.updateLotNo,
        data: {
          drugsLotId: drugsLotId,
          orderDetailsId: this.orderDetailsId
        },
        success(me, data) {

          me.$nextTick(() => {
            me.$refs["orderDetails"].refresh(false)
          })
        }
      })

    },
    processImg(record) {
      this.processInstanceId = record.processInstanceId
      this.processImageVisible = true
    },
    updateSettle(record) {
      this.http.confirm(this, {
        content: "确认更新结算信息吗?",
        url: api.bocr.createSettle,
        data: {
          returnId: record.returnId
        }
      })
    },
    //表单赋值
    setData(record) {
      this.formItem = this.getFormItem()
      this.returnColumns = this.getReturnColumns()
      this.queryParam.orderId = record.orderId
      const orderDetailsId = record.orderDetailsId
      if (record && record.orderId) {
        let params = {
          orderId: record.orderId,
          orderDetailsId: orderDetailsId,
          handlerName: "orderGetHandler"
        }
        this.http.get(this, {
          url: this.api.busOrder.get,
          params: params,
          success: function(me, data) {
            me.formItem = data
            me.loaded = true
            me.orderDetailsColumns = me.getOrderDetailsColumns()
            me.$nextTick(() => {
              me.$refs["returnDetails"].refresh(false)
            })
          }
        })

        this.$refs["orderDetails"].refresh(false)
        if (record.supplierBondType > 0) {
          this.http.get(this, {
            url: this.api.payment.get,
            params: {
              orderId: record.orderId,
              handlerName: "supplierBondPaymentGetHandler",
              amountType: "SupplierBond"
            },
            success: function(me, data) {

              me.supplierBond = data
            }
          })
        }

      }
    }
  },
  mounted() {

  },
  created() {
  },
  components: { ProcessImage, OrderCalcuateDrawer, SelectDrugsLotModal }
}
</script>

<style scoped lang='less' rel='stylesheet/less'>

</style>
